
//
//  WBStatusListViewModel.swift
//  新浪微博练习
//
//  Created by 韩旭 on 16/12/4.
//  Copyright © 2016年 韩旭. All rights reserved.
//

import UIKit
import SDWebImage
class WBStatusListViewModel: NSObject {

    //数据源数组
    lazy var statusViewModelArray: [WBStatusViewModel] = []
    
    func loadStatus (isPullDown: Bool, callBack: @escaping (Bool)->()) {
        var since_id = statusViewModelArray.first?.statusModel.id ?? "0"
        var max_id = statusViewModelArray.last?.statusModel.id ?? "0"
        if isPullDown{
            max_id = "0"
        }else{
            since_id = "0"

    }
        
        NetworkTool.shared.requestStatus(since_id:since_id, max_id: max_id, access_token: WBUserAccount.shared.access_token, callBack: {
            (statusObject) in
            
            //将statusObject 转成wbstatusModel 的数组
            if let statusObject = statusObject as? [String: Any],
                let statusDicArray = statusObject["statuses"] as? [[String: Any]],
                let statusModelArray = NSArray.yy_modelArray(with: WBStatusModel.self, json: statusDicArray) as? [WBStatusModel]  {
                
                //1.创建一个调度组
                let group = DispatchGroup()
                
                var responseViewModelArray: [WBStatusViewModel] = []
                //数组拼接
                for viewModel in statusModelArray {
                    let statusViewModel = WBStatusViewModel(statusModel: viewModel)
                    responseViewModelArray.append(statusViewModel)
                    print(viewModel)
                    //2,判断是否是单张图片
                    if  let pic_urls = statusViewModel.pic_urls,pic_urls.count == 1 {
                        group.enter()
                        //异步下载单张图片
                        if let urlString = pic_urls[0].thumbnail_pic,
                            let url = URL(string: urlString ){
                            SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (downloadImage, _, _, _, _) in
                                //计算单张图片的size
                                if let downloadImage = downloadImage {
                                    
                                    var size = downloadImage.size
                                    size.width = size.width * screenScale
                                    size.height = size.height * screenScale
                                    statusViewModel.pictureViewSize = size
                                }
                                group.leave()
                            })
                        }
                    }
                }
                //下拉刷新数据拼接
                if isPullDown {
                    self.statusViewModelArray = responseViewModelArray + self.statusViewModelArray
                }else{
                    if self.statusViewModelArray.count > 0
                    {
                        responseViewModelArray.removeFirst()
                    }
                    
                    self.statusViewModelArray += responseViewModelArray
                }
                ////a>4
                group.notify(queue: DispatchQueue.main){
                    //加载完成后的上下拉控件的处理
                    callBack(true)
              }
            }
            else{
                 callBack(false)
            }
        })
    }
}
